home *** CD-ROM | disk | FTP | other *** search
- /*
- File: MoreTextUtils.cp
-
- Contains:
-
- Written by: Pete Gontier
-
- Copyright: Copyright (c) 1998 Apple Computer, Inc., All Rights Reserved.
-
- You may incorporate this Apple sample source code into your program(s) without
- restriction. This Apple sample source code has been provided "AS IS" and the
- responsibility for its operation is yours. You are not permitted to redistribute
- this Apple sample source code as "Apple sample source code" after having made
- changes. If you're going to re-distribute the source, we require that you make
- it clear in the source that the code was descended from Apple sample source
- code, but that you've made changes.
-
- Change History (most recent first):
-
- <4> 21/4/99 Quinn Added ValidStringListHandle.
- <3> 2/15/99 PCG InlineGetHandleSize loses the Inline
- <2> 11/11/98 PCG fix header
- <1> 11/10/98 PCG first big re-org at behest of Quinn
-
- Old Change History (most recent first):
-
- <2> 10/23/98 PCG add GetStringFromLongDouble
- <1> 6/16/98 PCG initial checkin
- */
-
- #include "MoreTextUtils.h"
-
- #include <Resources.h>
- #include <PLStringFuncs.h>
-
- pascal OSErr NewStringPtr (ConstStr255Param init, UInt8 maxSize, StringPtr *result)
- {
- OSErr err = noErr;
-
- if (!MoreAssert (result && (!init || (*init <= maxSize))))
- err = paramErr;
- else
- {
- *result = StringPtr (NewPtr (1 + (maxSize ? maxSize : (init ? *init : 255))));
-
- if (!*result)
- err = MemError ( );
- else if (init && *init)
- PLstrcpy (*result,init);
- else
- **result = 0;
- }
-
- return err;
- }
-
- pascal OSErr NewStringListHandle (Handle *h)
- {
- DebugStr ("\pThis routine has never been traced.");
-
- if (!MoreAssert (h))
- return nilHandleErr;
- *h = NewHandleClear (sizeof (UInt16));
- if (!*h)
- return MemError ( );
- return noErr;
- }
-
- pascal OSErr AppendStringToListHandle (ConstStr255Param str, Handle h)
- {
- DebugStr ("\pThis routine has never been traced.");
-
- OSErr err = noErr;
-
- if (!MoreAssert (h && *h))
- err = nilHandleErr;
- else
- {
- UInt8 dummyZero;
-
- if (!str)
- {
- dummyZero = 0;
- str = &dummyZero;
- }
-
- if (!(err = PtrAndHand (str, h, *str + 1)))
- (** (UInt16 **) h) += 1;
- }
-
- return err;
- }
-
- pascal OSErr GetNewStringList (short resID, tStringListP *newStringList)
- {
- OSErr err = noErr;
-
- Handle h = GetResource ('STR#',resID);
-
- if (!h)
- {
- err = ResError ( );
- if (!err) err = resNotFound;
- }
- else
- {
- Size handleSize = GetHandleSize (h);
- (void) MoreAssert (MemError ( ) == noErr);
-
- if (handleSize < 2)
- err = paramErr;
- else
- {
- UInt16 stringCount = ** (UInt16 **) h;
- Size stringListSize = sizeof (**newStringList) + (handleSize - 2) +
- (stringCount * sizeof (StringPtr));
-
- *newStringList = (tStringListP) NewPtr (stringListSize);
-
- if (!*newStringList)
- err = MemError ( );
- else
- {
- (*newStringList)->count = stringCount;
-
- if (stringCount)
- {
- UInt16 index = 0;
-
- StringPtr stringScan = (StringPtr) ((*newStringList)->list + stringCount);
-
- BlockMoveData (2 + *h, stringScan, handleSize - 2);
-
- do
- {
- (*newStringList)->list [index] = stringScan;
- stringScan += *stringScan + 1;
- }
- while (++index < stringCount);
- }
- }
- }
-
- ReleaseResource (h);
- (void) MoreAssert (ResError ( ) == noErr);
- }
-
- return err;
- }
-
- pascal Boolean ValidStringListHandle(Handle stringList)
- // See comment in interface part.
- {
- Boolean result;
- UInt16 stringCount;
- UInt16 stringIndex;
- UInt8* cursor;
- UInt8* bound;
- ByteCount stringListLength;
-
- result = true;
- if (stringList == NULL || *stringList == NULL || GetHandleSize(stringList) < sizeof(UInt16) ) {
- result = false;
- }
- if (result) {
- stringCount = *((UInt16 *) *stringList);
-
- stringListLength = GetHandleSize(stringList);
-
- // From here down we have to make sure we do nothing to move
- // or purge until we're done with cursor and bound.
-
- cursor = ((UInt8 *)*stringList) + sizeof(UInt16);
- bound = ((UInt8 *)*stringList) + stringListLength;
- for (stringIndex = 0; stringIndex < stringCount; stringIndex++) {
- if ( cursor < bound ) {
- cursor += *cursor + 1;
- } else {
- result = false;
- break;
- }
- }
-
- if (result) {
- if ( cursor != bound ) {
- result = false;
- }
- }
- }
-
- return result;
- }
-
- pascal OSErr GetPascalStringFromLongDouble (long double d, SInt8 precision, StringPtr buf)
- {
- OSErr err = noErr;
-
- *buf = 0;
-
- // If client requests more precision than is available, bitch.
-
- if (!MoreAssert (precision < SIGDIGLEN))
- err = paramErr;
- else
- {
- // If precision is less than 0, client is requesting
- // as much precision as is available.
-
- if (precision < 0)
- precision = SIGDIGLEN;
-
- // Try normal notation, and if that overflows,
- // fall back to scientific notation (bleah!).
-
- decimal dec;
- decform df = { 1, 0, precision };
-
- num2dec (&df,d,&dec);
-
- if (dec.sig.text [0] == '?')
- {
- df.style = 0;
- df.digits = SIGDIGLEN;
-
- num2dec (&df,d,&dec);
- }
-
- if (*(dec.sig.text) == '0')
- PLstrcpy (buf,"\p0");
- else if (*(dec.sig.text) == 'I')
- PLstrcpy (buf,"\p[INF]");
- else if (*(dec.sig.text) == 'N')
- PLstrcpy (buf,"\p[NaN]");
- else
- {
- // Finally, convert it to a pascal string...
-
- dec2str (&df,&dec,(Ptr)buf);
- c2pstr ((Ptr)buf);
-
- // ...and trim trailing zeroes.
-
- // if (df.style == 1 && dec.exp < 0)
- // while (buf [*buf] == '0')
- // *buf -= 1;
- }
- }
-
- return err;
- }
-